package br.ufrn.geologia.ggemma.model.extension;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.jspresso.framework.model.component.AbstractComponentExtension;
import org.jspresso.framework.util.bean.IPropertyChangeCapable;

import br.ufrn.geologia.ggemma.model.User;

/**
 * Helper class computing extended properties for User entity.
 */
public class UserExtension extends AbstractComponentExtension<User> {
	
	private String fullName = null;
	
	/**
	 * Constructs a new <code>UserExtension</code> instance.
	 * 
	 * @param extendedUser
	 *            The extended user instance.
	 */
	public UserExtension(User extendedUser) {
		super(extendedUser);
		
		extendedUser.addPropertyChangeListener("fullName", 
			new PropertyChangeListener() {
				public void propertyChange(PropertyChangeEvent evt) {
					String oldFullName = fullName;
					fullName = null; 
					getComponent().firePropertyChange("fullName", oldFullName, IPropertyChangeCapable.UNKNOWN); 
				}
		});
	}

	/**
	 * Computes the Users full name.
	 * 
	 * @return The Users full name.
	 */
	public String getFullName() {
		if (fullName != null) {
			return fullName;
		}
		fullName = getComponent().getFirstName() + " " + getComponent().getLastName();
		fullName = fullName.replace("null", "").replace("  ", " ");
		return fullName;
	}
}
